package com.t216.dm216.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.t216.dm216.mapper.DmSchedulerSeatMapper;
import com.t216.dm216.pojo.DmSchedulerSeat;
import com.t216.dm216.pojo.DmSchedulerSeatPrice;
import com.t216.dm216.pojo.dto.DmSchedulerSeatDTO;
import com.t216.dm216.pojo.dto.DmSeatDTO;
import com.t216.dm216.pojo.dto.DmSeatInfoDTO;
import com.t216.dm216.service.DmSchedulerSeatPriceService;
import com.t216.dm216.service.DmSchedulerSeatService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class DmSchedulerSeatServiceImpl implements DmSchedulerSeatService {
    @Resource
    private DmSchedulerSeatMapper dmSchedulerSeatMapper;
    @Resource
    private DmSchedulerSeatPriceService dmSchedulerSeatPriceService;
    @Override
    public DmSchedulerSeatDTO getSeatArray(DmSeatDTO seatDTO) {
        MPJLambdaWrapper<DmSchedulerSeat> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(DmSchedulerSeat::getScheduleId, seatDTO.getScheduleId());
        queryWrapper.select("GROUP_CONCAT(CASE areaLevel WHEN 1 THEN 'a' WHEN 2 THEN 'b' WHEN 3 THEN 'c' ELSE '_' END)");
        queryWrapper.groupBy(DmSchedulerSeat::getX);
        List<String> seatArray = dmSchedulerSeatMapper.selectObjs(queryWrapper);
        DmSchedulerSeatDTO dto = new DmSchedulerSeatDTO();
        dto.setSeatArray(seatArray);
        dto.setCinemaId(seatDTO.getCinemaId());
        return dto;
    }

    @Override
    public DmSeatDTO getCinemaSeatArrayByScheduleId(DmSeatDTO seatDTO) {
        List<DmSchedulerSeatPrice> seatPriceList = dmSchedulerSeatPriceService.getSeatPriceList(seatDTO.getScheduleId());
        seatDTO.setSeatPriceList(seatPriceList);

        MPJLambdaWrapper<DmSchedulerSeat> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.select(DmSchedulerSeat::getId, DmSchedulerSeat::getX, DmSchedulerSeat::getY, DmSchedulerSeat::getAreaLevel, DmSchedulerSeat::getSort, DmSchedulerSeat::getStatus);
        queryWrapper.eq(DmSchedulerSeat::getScheduleId,seatDTO.getScheduleId());
        List<DmSeatInfoDTO> seatInfoList = dmSchedulerSeatMapper.selectJoinList(DmSeatInfoDTO.class,queryWrapper);
        seatInfoList.forEach(seatInfoDTO -> seatInfoDTO.setCinemaId(seatDTO.getCinemaId()));
        seatDTO.setSeatInfoList(seatInfoList);

        return seatDTO;
    }

    @Override
    public boolean updateSeatByOrder(DmSchedulerSeat schedulerSeat) {
        LambdaUpdateWrapper<DmSchedulerSeat> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(DmSchedulerSeat::getOrderNo, schedulerSeat.getOrderNo());
        updateWrapper.set(DmSchedulerSeat::getUserId, schedulerSeat.getUserId());
        updateWrapper.set(DmSchedulerSeat::getStatus,2);
        updateWrapper.eq(DmSchedulerSeat::getId,schedulerSeat.getId());

        return dmSchedulerSeatMapper.update(updateWrapper)>0;
    }
}
